package org.snail.employee.service.impl;

import lombok.NonNull;
import org.apache.log4j.Logger;
import org.snail.employee.bean.*;
import org.snail.employee.mapper.EmployeesMapper;
import org.snail.exception.JsonResult;
import org.snail.employee.service.AddressService;
import org.snail.employee.mapper.AddressMapper;
import org.snail.other.bean.GroupDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 数据库操作
 */
@Service
public class AddressServiceImpl implements AddressService {

    private static final Logger LOGGER =  Logger.getLogger(AddressServiceImpl.class);
    @Autowired
    private AddressMapper addressDao;
    @Autowired
    private EmployeesMapper employeesMapper;

    /**
     * 添加群成员
     * @param addressDOList
     */
    public JsonResult insertAddressList(List<AddressDO> addressDOList){
            JsonResult result = new JsonResult();
            for(AddressDO addressDO : addressDOList){
                //addressId 处理同一个群重复添加同一个人
                Integer addressId = addressDao.findAddressIdByEmpIdAndGroupId(addressDO);
                if(addressId==null) {
                    addressDao.insertAddress(addressDO);
                }
            }
            return result;
    }
    /**
     * 添加群成员
     * @param empIds
     * @Param groupId    群组id
     */
    public JsonResult insertAddressList(List<Integer> empIds,@NonNull Long groupId){
        JsonResult result = new JsonResult();
        AddressDO addressDO = new AddressDO();
        addressDO.setGroupId(groupId);
        for(Integer empId : empIds){
            addressDO.setEmpId(empId);
        }
        result.setCode(200);
        return result;
    }

    /**
     * 从群里删成员
     * @param addressList
     */
    public JsonResult deleteAddress(List<AddressCondition> addressList){
        JsonResult result = new JsonResult();
        for(AddressCondition addressDO : addressList){
            addressDao.deleteAddress(addressDO);
        }
        return result;
    }


    /**
     * 从群里删成员
     * @param neeDelUserIds
     * @param groupDO
     */
    public void deleteAddress(List<String> neeDelUserIds, GroupDO groupDO){
        addressDao.deleteAddressListByDingID(neeDelUserIds, groupDO.getId(), new Date());
    }



    public List<String> findDIngUserIdsByGroupId(Long groupId) {
        return addressDao.findDIngUserIdsByGroupId(groupId);
    }


    @Override
    public List<Long> findGroupIds() {
        return addressDao.findGroupIds();
    }

    /**
     * 专门用于测试异常考勤，其他时间禁止使用
     */
    public void testAddress() {
//        EmpCondition empCondition = new EmpCondition();
//        List<EmployeesDO> allEmp = employeesMapper.findAllEmp(empCondition);
//        for (int i = 0 ; i < allEmp.size(); i++) {
//            EmployeesDO employeesDO = allEmp.get(i);
//            Integer empId = employeesDO.getEmpId();
//            int groupId = 0;
//            if (i%3 == 0) {
//                groupId = 61;
//            }else if (i%3 == 1) {
//                 groupId = 62;
//            }else if (i%3 == 2) {
//                 groupId = 63;
//            }
//            AddressDO addressDO = new AddressDO();
//            addressDO.setEmpId(empId);
//            addressDO.setGroupId( groupId);
//            addressDao.insertAddress(addressDO);
//        }
    }

}
